<template>
  <div class="zodiac-fortune">
    <a-card class="fortune-card" :bordered="false">
      <template #title>
        <div class="title">
          <span>✨ 星座运势查询 ✨</span>
        </div>
      </template>

      <div class="zodiac-selector">
        <div class="selector-label">选择星座</div>
        <div class="zodiac-buttons">
          <a-radio-group
            v-model:value="selectedZodiac"
            button-style="solid"
            @change="fetchZodiacData"
          >
            <a-radio-button
              v-for="zodiac in zodiacSigns"
              :key="zodiac.name"
              :value="zodiac.name"
            >
              {{ zodiac.icon }} {{ zodiac.name }}
            </a-radio-button>
          </a-radio-group>
        </div>

        <a-input-search
          v-model:value="customZodiac"
          placeholder="输入星座名称"
          enter-button="查询"
          size="large"
          class="custom-search"
          @search="searchCustomZodiac"
          :loading="loading"
        />
      </div>

      <div v-if="loading" class="loading-container">
        <a-spin tip="正在获取星座数据...">
          <div class="loading-content">
            <a-skeleton active :paragraph="{ rows: 8 }" />
          </div>
        </a-spin>
      </div>

      <div v-else-if="zodiacData" class="fortune-content">
        <div class="zodiac-header">
          <div class="zodiac-avatar">
            <span class="zodiac-icon">{{ getZodiacIcon(zodiacData.xz) }}</span>
          </div>
          <div class="zodiac-info">
            <h2>{{ zodiacData.xz }}座</h2>
            <p class="zodiac-tip">{{ zodiacData.ts }}</p>
          </div>
        </div>

        <div class="lucky-info">
          <div class="lucky-item">
            <div class="lucky-icon">🧭</div>
            <div class="lucky-label">贵人方位</div>
            <div class="lucky-value">{{ zodiacData.grfw }}</div>
          </div>

          <div class="lucky-item">
            <div class="lucky-icon">♈</div>
            <div class="lucky-label">贵人星座</div>
            <div class="lucky-value">{{ zodiacData.grxz }}</div>
          </div>

          <div class="lucky-item">
            <div class="lucky-icon">🔢</div>
            <div class="lucky-label">幸运数字</div>
            <div class="lucky-value">{{ zodiacData.xysz }}</div>
          </div>

          <div class="lucky-item">
            <div class="lucky-icon">🎨</div>
            <div class="lucky-label">幸运颜色</div>
            <div class="lucky-value">
              <div class="color-dot" :style="{ backgroundColor: getLuckyColor(zodiacData.xyys) }"></div>
              {{ zodiacData.xyys }}
            </div>
          </div>
        </div>

        <a-tabs default-active-key="1" class="fortune-tabs">
          <a-tab-pane key="1" tab="整体运势">
            <div class="fortune-detail">
              <div class="detail-icon">🔮</div>
              <div class="detail-content">{{ zodiacData.ztys }}</div>
            </div>
          </a-tab-pane>

          <a-tab-pane key="2" tab="爱情运势">
            <div class="fortune-detail">
              <div class="detail-icon">❤️</div>
              <div class="detail-content">{{ zodiacData.aqys }}</div>
            </div>
          </a-tab-pane>

          <a-tab-pane key="3" tab="事业运势">
            <div class="fortune-detail">
              <div class="detail-icon">💼</div>
              <div class="detail-content">{{ zodiacData.syys }}</div>
            </div>
          </a-tab-pane>

          <a-tab-pane key="4" tab="财富运势">
            <div class="fortune-detail">
              <div class="detail-icon">💰</div>
              <div class="detail-content">{{ zodiacData.cfys }}</div>
            </div>
          </a-tab-pane>
        </a-tabs>

        <div class="share-section">
          <a-button type="primary" @click="shareFortuneData">
            <template #icon><share-alt-outlined /></template>
            分享运势
          </a-button>

          <a-button @click="fetchZodiacData">
            <template #icon><reload-outlined /></template>
            刷新数据
          </a-button>
        </div>

        <div class="update-time">
          最后更新: {{ lastUpdateTime }}
        </div>
      </div>

      <div v-else class="empty-state">
        <a-empty description="暂无星座数据">
          <template #image>
            <img src="https://gw.alipayobjects.com/zos/antfincdn/ZHrcdLPrvN/empty.svg" alt="empty" />
          </template>
          <template #description>
            <span>请选择星座或输入星座名称进行查询</span>
          </template>
        </a-empty>
      </div>
    </a-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { message } from 'ant-design-vue';
import axios from 'axios';
import { ShareAltOutlined, ReloadOutlined } from '@ant-design/icons-vue';

// 状态定义
const zodiacData = ref(null);
const loading = ref(false);
const selectedZodiac = ref('白羊');
const customZodiac = ref('');
const lastUpdateTime = ref('');

// 星座列表
const zodiacSigns = [
  { name: '白羊', icon: '♈' },
  { name: '金牛', icon: '♉' },
  { name: '双子', icon: '♊' },
  { name: '巨蟹', icon: '♋' },
  { name: '狮子', icon: '♌' },
  { name: '处女', icon: '♍' },
  { name: '天秤', icon: '♎' },
  { name: '天蝎', icon: '♏' },
  { name: '射手', icon: '♐' },
  { name: '摩羯', icon: '♑' },
  { name: '水瓶', icon: '♒' },
  { name: '双鱼', icon: '♓' }
];

// 获取星座数据
const fetchZodiacData = async () => {
  loading.value = true;

  try {
    const response = await axios.get('https://v.api.aa1.cn/api/xingzuo/', {
      params: {
        msg: selectedZodiac.value
      }
    });

    if (response.data && response.data.code === 1) {
      zodiacData.value = response.data;
      lastUpdateTime.value = new Date().toLocaleString('zh-CN');
      message.success(`${selectedZodiac.value}座运势获取成功`);
    } else {
      message.error(response.data?.msg || '获取星座数据失败');
    }
  } catch (error) {
    console.error('获取星座数据失败:', error);
    message.error('获取星座数据失败，请稍后重试');
  } finally {
    loading.value = false;
  }
};

// 搜索自定义星座
const searchCustomZodiac = () => {
  if (!customZodiac.value) {
    message.warning('请输入星座名称');
    return;
  }

  selectedZodiac.value = customZodiac.value;
  fetchZodiacData();
};

// 获取星座对应的图标
const getZodiacIcon = (zodiacName) => {
  const zodiac = zodiacSigns.find(z => z.name === zodiacName);
  return zodiac ? zodiac.icon : '✨';
};

// 获取幸运颜色的CSS颜色值
const getLuckyColor = (colorName) => {
  const colorMap = {
    '红': '#f5222d',
    '橙': '#fa8c16',
    '黄': '#fadb14',
    '绿': '#52c41a',
    '青': '#13c2c2',
    '蓝': '#1890ff',
    '紫': '#722ed1',
    '粉': '#eb2f96',
    '樱桃红': '#c41d7f',
    '玫瑰红': '#eb2f96',
    '藏青': '#003a8c',
    '深蓝': '#003a8c',
    '浅蓝': '#91d5ff',
    '天蓝': '#1890ff',
    '深绿': '#237804',
    '浅绿': '#b7eb8f',
    '棕': '#a0522d',
    '灰': '#8c8c8c',
    '白': '#ffffff',
    '黑': '#000000'
  };

  // 模糊匹配颜色名称
  for (const key in colorMap) {
    if (colorName.includes(key)) {
      return colorMap[key];
    }
  }

  return '#1890ff'; // 默认颜色
};

// 分享运势数据
const shareFortuneData = () => {
  if (!zodiacData.value) return;

  const shareText = `
${zodiacData.value.xz}座今日运势：
${zodiacData.value.ts}
幸运数字：${zodiacData.value.xysz}
幸运颜色：${zodiacData.value.xyys}
贵人方位：${zodiacData.value.grfw}
贵人星座：${zodiacData.value.grxz}
  `;

  if (navigator.share) {
    navigator.share({
      title: `${zodiacData.value.xz}座今日运势`,
      text: shareText
    })
      .then(() => message.success('分享成功'))
      .catch((error) => {
        console.error('分享失败:', error);
        copyToClipboard(shareText);
      });
  } else {
    copyToClipboard(shareText);
  }
};

// 复制到剪贴板
const copyToClipboard = (text) => {
  navigator.clipboard.writeText(text)
    .then(() => {
      message.success('运势信息已复制到剪贴板');
    })
    .catch(() => {
      message.error('复制失败，请手动复制');
    });
};

// 页面加载时获取数据
onMounted(() => {
  fetchZodiacData();
});
</script>

<style scoped>
.zodiac-fortune {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
}

.fortune-card {
  border-radius: 16px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  background: linear-gradient(to bottom, #ffffff, #f9f0ff);
}

.title {
  font-size: 1.8rem;
  text-align: center;
  background: linear-gradient(45deg, #722ed1, #eb2f96);
  -webkit-background-clip: text;
  background-clip: text;

  font-weight: bold;
  padding: 8px 0;
}

.zodiac-selector {
  margin-bottom: 24px;
}

.selector-label {
  font-size: 1.1rem;
  font-weight: bold;
  margin-bottom: 12px;
  color: #333;
}

.zodiac-buttons {
  margin-bottom: 16px;
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
}

.custom-search {
  margin-top: 16px;
}

.loading-container, .empty-state {
  padding: 60px 0;
  text-align: center;
}

.loading-content {
  padding: 20px;
  background-color: rgba(255, 255, 255, 0.8);
  border-radius: 12px;
}

.fortune-content {
  animation: fadeIn 0.5s ease-in-out;
}

.zodiac-header {
  display: flex;
  align-items: center;
  gap: 20px;
  margin-bottom: 24px;
  padding: 16px;
  background-color: rgba(255, 255, 255, 0.7);
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.zodiac-avatar {
  width: 80px;
  height: 80px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(45deg, #722ed1, #eb2f96);
  border-radius: 50%;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.zodiac-icon {
  font-size: 2.5rem;
  color: white;
}

.zodiac-info {
  flex: 1;
}

.zodiac-info h2 {
  margin: 0 0 8px 0;
  font-size: 1.8rem;
  color: #333;
}

.zodiac-tip {
  margin: 0;
  font-size: 1rem;
  color: #666;
  font-style: italic;
}

.lucky-info {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}

.lucky-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  padding: 16px;
  background-color: rgba(255, 255, 255, 0.7);
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  transition: all 0.3s;
}

.lucky-item:hover {
  transform: translateY(-3px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.lucky-icon {
  font-size: 1.8rem;
  margin-bottom: 8px;
}

.lucky-label {
  font-weight: bold;
  margin-bottom: 8px;
  color: #333;
}

.lucky-value {
  font-size: 1rem;
  color: #666;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.color-dot {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  border: 1px solid #d9d9d9;
}

.fortune-tabs {
  margin-bottom: 24px;
}

.fortune-detail {
  display: flex;
  align-items: flex-start;
  gap: 16px;
  padding: 16px;
  background-color: rgba(255, 255, 255, 0.7);
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.detail-icon {
  font-size: 2rem;
  line-height: 1;
}

.detail-content {
  flex: 1;
  font-size: 1rem;
  line-height: 1.6;
  color: #333;
}

.share-section {
  display: flex;
  justify-content: center;
  gap: 16px;
  margin-bottom: 16px;
}

.update-time {
  text-align: center;
  font-size: 0.9rem;
  color: #999;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}

@media (max-width: 768px) {
  .zodiac-fortune {
    padding: 10px;
  }

  .title {
    font-size: 1.5rem;
  }

  .zodiac-header {
    flex-direction: column;
    text-align: center;
  }

  .zodiac-info h2 {
    font-size: 1.5rem;
  }

  .lucky-info {
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  }

  .share-section {
    flex-direction: column;
    gap: 8px;
  }
}
</style>
